home *** CD-ROM | disk | FTP | other *** search
- Path: magnus.acs.ohio-state.edu!csn!ub!newserve!rebecca!rpi!not-for-mail
- From: tonyh@tcp.co.uk
- Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
- Subject: Re: Q: Rigorous coding using #if !defined(...) and #include
- Date: 2 Feb 1996 23:58:03 -0000
- Organization: UWL
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: herbs@connobj.com
- Message-ID: <4eu8eb$npt@netlab.cs.rpi.edu>
- References: <4eo1fs$rr3@netlab.cs.rpi.edu>
- Reply-To: tonyh@tcp.co.uk
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: Fri, 02 Feb 1996 20:50:03 GMT
-
- On 31 Jan 1996 15:22:36 -0000 David Carr wrote:
-
- > Okay, nuff of the preamble. The problem is this. I have
- > another class;
- >
- > #if !defined (__OTHER_H)
- > #define (__OTHER_H)
- >
- > #include "MyClass.h"
- >
- > class Other
- > {
- >
- ...// Uses something in MyClass.h
- > };
- >
- > #endif
- >
- > and in my source for MyClass.cpp, MyClass.h is obviously
- > included. Now,
- > when I try to compile MyClass.cpp, MyClass.h is included and
- > __MYCLASS_H
- > becomes defined. Then, MyClass.h #includes Other.h, thus
- > defining __OTHER_H.
- > Now, Other.h #includes MyClass.h. As __MYCLASS_H has already
- > been defined,
- > the code in MyClass.h is not included. Returning to Other.h,
- > this code
- > references something in MyClass.h (i.e. its class) and a
- > compiler error is
- > generated because the symbol/identifier/whatever is unknown.
-
- It sounds like either you haven't split up your declarations/definitions
- into enough header files or you've got some circular dependencies that
- wouldn't work anyway.
-
- If you have something like:
-
- class A {
- B *p;
- public:
- do_something();
- };
-
- class B {
- public:
- void use_a(A *p)
- {
- a->do_something();
- }
- };
-
- class B needs a definition of class A because it accesses one or more
- members. But class A only has a pointer to B, so it only needs to know
- that there is some class called B, without knowing its definition ie
- change the member to:
-
- class B *p;
-
- or declare:
-
- class B;
-
- before A's definition.
-
- You can't have two classes that use each other's members unless you do
- it through an abstract interface class.
-
- --
- -------------------------------------------------------------------
- Tony Houghton tonyh@tcp.co.uk
- Using a RiscPC 700 http://www.tcp.co.uk/~tonyh
- -------------------------------------------------------------------
- You don't call cancer a 'popular' disease,
- so why call Windows a popular operating system?
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-